﻿/***************************
* 1.航班
* 2.列车
* 3.客运
****************************/

/* 变量 */
var flight = "traffic/flight.html";
var flightDetail = "traffic/flightDetail.html";

var train = "traffic/train.html";
var trainDetail = "traffic/trainDetail.html";

var coach = "traffic/coach.html";
var coachDetail = "traffic/coachDetail.html";

/* 航班首页  */
var contactsCallback_flight = myApp.onPageInit('flight', function (page) {

    $$(".cui-arrow-close .cui-city-t").on("click", function () {
        var _index = $$(this).parent(".cui-arrow-close").index();
        _index = _index > 0 ? _index - 1 : 0;
        $$(".cui-arrow-close .cui-city-n").hide();
        if ($$(".cui-arrow-close ul").eq(_index).css("display") == "none") {
            $$(".cui-arrow-close ul").eq(_index).show();
        }
        else {
            $$(".cui-arrow-close ul").eq(_index).hide();
        }
    });

    if (document.getElementById('data_city') != null) {
        document.getElementById('data_city').onscroll = function () {
            $$(this).find(".flight-ctltsfixed").css("top", this.scrollTop + "px");
        };
    }

    var base64 = new Base64();
    var flag_type = 1; //1：出发,2：到达

    //出发站
    $$("#single_start").on("click", function () {
        flag_type = 1;
        myApp.popup(".popup-flight_city");
    });

    //到站
    $$("#single_end").on("click", function () {
        flag_type = 2;
        myApp.popup(".popup-flight_city");
    });

    $$(".popup-flight_city .city-item").on("click", function () {
        var _data_name = $$(this).attr("data-name");
        if (_data_name) {
            if (flag_type == 1)
                $$("#single_start").val(_data_name);
            else
                $$("#single_end").val(_data_name);

            myApp.closeModal(".popup-flight_city");
        }
    });

    //换出发 - 到站
    $$(".station-change").on("click", function () {
        if ($$('#stationStart').val() != "" && $$('#single_end').val() != "") {
            var v_temp = $$('#single_start').val();
            $$('#single_start').val($$('#single_end').val());
            $$('#single_end').val(v_temp);
        }
    });

    //航班查询 
    $$("#btn_search_flight").on("click", function () {
        if ($$("#single_start").val() == "") {
            myApp.alert('请输入起点站');
            return;
        }

        if ($$("#single_end").val() == "") {
            myApp.alert('请输入终点站');
            return;
        }

        if ($$("#flight-calendar-date-format").val() == "") {
            myApp.alert('请输入出发日期');
            return;
        }
        mainView.router.loadPage("flightDetail.html?date=" + $$("#flight-calendar-date-format").val() + "&start=" + base64.encode($$("#single_start").val()) + "&end=" + base64.encode($$("#single_end").val()));
    });

    DatepickerFun("#flight-calendar-date-format", 0);

});

/* 航班查询结果  */
var contactsCallback_flightDetail = myApp.onPageInit('flightDetail', function (page) {
    var base64 = new Base64();
    var _start = ISS.utils.getParameter("start");
    var _end = ISS.utils.getParameter("end");
    var _date = ISS.utils.getParameter("date");
    if (page != undefined) {
        _start = page.query.start;
        _end = page.query.end;
        _date = page.query.date;
    }

    var _url = ISS_Constants.Url.const_flight_station + "&start=" + base64.decode(_start) + "&end=" + base64.decode(_end) + "&date=" + _date;
    $$("#title").text(base64.decode(_start) + "-" + base64.decode(_end));

    myApp.showPreloader();
    $$.ajax({
        url: _url + "",
        crossDomain: true,
        dataType: "json",
        success: function (data, status) {
            myApp.hidePreloader();
            if (data.resultcode == "200" && data.result && data.result.length > 0) {
                var searchTemplate = $$('#template-flight-detail').html();
                var compiledTemplate = Template7.compile(searchTemplate);
                var html = compiledTemplate(data);
                $$("#flight_list").html(html);
            }
            else {
                //没有数据
                $$("#flight_list").html("<li style='text-align:center;height:5em;line-height:5em;'>没有查到相关航班!</li>");
            }
        },
        error: function (xhr, status) {
            myApp.hidePreloader();
            $$("#flight_list").html("<li style='text-align:center;height:5em;line-height:5em;'>没有查到相关航班!</li>");
        }
    });
});

/* 火车首页  */
var contactsCallback_train = myApp.onPageInit('train', function (page) {

    // 控制
    $$(".cui-arrow-close .cui-city-t").on("click", function () {
        var _index = $$(this).parent(".cui-arrow-close").index();
        _index = _index > 0 ? _index - 1 : 0;
        $$(".cui-arrow-close .cui-city-n").hide();
        if ($$(".cui-arrow-close ul").eq(_index).css("display") == "none") {
            $$(".cui-arrow-close ul").eq(_index).show();
        }
        else {
            $$(".cui-arrow-close ul").eq(_index).hide();
        }
    });

    if (document.getElementById('data-train_city') != null) {
        document.getElementById('data-train_city').onscroll = function () {
            $$(this).find(".flight-ctltsfixed").css("top", this.scrollTop + "px");
        };
    }

    var base64 = new Base64();
    var flag_type = 1; //1：出发,2：到达
    //出发站
    $$("#stationStart").on("click", function () {
        flag_type = 1;
        myApp.popup("#data-train_city");
    });

    //到站
    $$("#stationEnd").on("click", function () {
        flag_type = 2;
        myApp.popup("#data-train_city");
    });

    // 选中
    $$(".popup-flight_city .cui-city-item").on("click", function () {
        var _data_name = $$(this).attr("data-name");
        if (_data_name) {
            if (flag_type == 1)
                $$("#stationStart").val(_data_name);
            else
                $$("#stationEnd").val(_data_name);

            myApp.closeModal("#data-train_city");
        }
    });

    //换出发 - 到站
    $$(".station-change").on("click", function () {
        if ($$('#stationStart').val() != "" && $$('#stationEnd').val() != "") {
            var v_temp = $$('#stationStart').val();
            $$('#stationStart').val($$('#stationEnd').val());
            $$('#stationEnd').val(v_temp);
        }
    });

    //车站查询 
    $$("#btn_search_station").on("click", function () {
        if ($$("#stationStart").val() == "") {
            myApp.alert('请输入起点站');
            return;
        }

        if ($$("#stationEnd").val() == "") {
            myApp.alert('请输入终点站');
            return;
        }

        if ($$("#calendar-date-format").val() == "") {
            myApp.alert('请输入出发日期');
            return;
        }

        var _gt = $$("#ckTranGT").is(":checked") ? 1 : 0;
        mainView.router.loadPage("trainDetail.html?start=" + base64.encode($$("#stationStart").val()) + "&end=" + base64.encode($$("#stationEnd").val()) + "&date=" + $$("#calendar-date-format").val() + "&flag=" + _gt);
    });

    //车次查询
    $$("#btn-search-number").on("click", function () {
        if ($$("#train_number").val() == "") {
            myApp.alert('请输入列车车次');
            return;
        }
        mainView.router.loadPage("trainDetail.html?train_number=" + $$("#train_number").val());
    });

    DatepickerFun("#calendar-date-format", 0);
});

/* 火车查询结果  */
var contactsCallback_trainDetail = myApp.onPageInit('trainDetail', function (page) {
    var base64 = new Base64();
    var _start = base64.decode(ISS.utils.getParameter("start"));
    var _end = base64.decode(ISS.utils.getParameter("end"));
    var _flag = ISS.utils.getParameter("flag");
    var _date = ISS.utils.getParameter("date");
    var _train_no = ISS.utils.getParameter("train_number");
    var _search_type = 0;  //0-站站查询，1-车次查询
    var _url = "";

    if (page != undefined) {
        _start = base64.decode(page.query.start);
        _end = base64.decode(page.query.end);
        _flag = page.query.flag;
        _date = page.query.date;
        _train_no = page.query.train_number;
    }

    if (!_train_no && _flag == 1) {
        _search_type = 0;
        _url = ISS_Constants.Url.const_train_station + "&from=" + _start + "&to=" + _end + "&date=" + _date + "&tt=G";
    }
    else if (!_train_no && _flag == 0) {
        _search_type = 0;
        _url = ISS_Constants.Url.const_train_station + "&from=" + _start + "&to=" + _end + "&date=" + _date;
    }
    else if (_train_no) {
        _search_type = 1;  //列车车次查询
        _url = ISS_Constants.Url.const_train_number + "&name=" + _train_no;
    }
    else {
        //没有任何参数
        return;
    }
    switch (_search_type) {
        case 0:
            $$("#title").text(_start + "-" + _end);
            break;
        case 1:
            $$("#title").text("车次查询");
            break;

    }

    myApp.showPreloader();
    $$.ajax({
        url: _url + "",
        crossDomain: true,
        method: "POST",
        dataType: "json",
        success: function (data, status) {
            myApp.hidePreloader();
            var str_buffer = new Array();
            switch (_search_type) {
                case 0:
                    {
                        //站站查询
                        if (data.reason == "成功的返回" && data.result && data.result.length > 0) {
                            var searchTemplate = $$('#template-train-tab1').html();
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#train_list").html(html);
                        }
                        else {
                            $$("#train_list").html("<li style='text-align:center;height:5em;line-height:5em;'>没有查到相关车次!</li>");
                        }
                        break;
                    }
                case 1:
                    {
                        //列车车次查询
                        if (data.error_code == "0" && data.result && data.result.train_info) {
                            var searchTemplate = $$('#template-train-tab2').html();
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data.result);
                            $$("#train_list").html(html);
                        }
                        else {
                            $$("#train_list").html("<li style='text-align:center;height:5em;line-height:5em;'>没有查到相关车次!</li>");
                        }
                        break;
                    }
            }
        },
        error: function (xhr, status) {
            myApp.hidePreloader();
            $$("#train_list").html("<li style='text-align:center;height:5em;line-height:5em;'>没有查到相关车次!</li>");
        }
    });
});

/* 客运首页  */
var contactsCallback_coach = myApp.onPageInit('coach', function (page) {

    // 控制
    $$(".cui-arrow-close .cui-city-t").on("click", function () {
        var _index = $$(this).parent(".cui-arrow-close").index();
        _index = _index > 0 ? _index - 1 : 0;
        $$(".cui-arrow-close .cui-city-n").hide();
        if ($$(".cui-arrow-close ul").eq(_index).css("display") == "none") {
            $$(".cui-arrow-close ul").eq(_index).show();
        }
        else {
            $$(".cui-arrow-close ul").eq(_index).hide();
        }
    });

    if (document.getElementById('data_city') != null) {
        document.getElementById('data_city').onscroll = function () {
            $$(this).find(".flight-ctltsfixed").css("top", this.scrollTop + "px");
        };
    }

    var flag_type = 1; //1：出发,2：到达
    //出发
    $$("#stationStart").on("click", function () {
        flag_type = 1;
        myApp.popup(".popup-flight_city");
    });
    //到达
    $$("#stationEnd").on("click", function () {
        flag_type = 2;
        myApp.popup(".popup-flight_city");
    });

    //选中热门城市列表
    $$(".popup-flight_city .cui-city-item").on("click", function () {
        $$(this).addClass("current");
        var _data_name = $$(this).attr("data-name");
        if (_data_name) {
            if (flag_type == 1)
                $$("#stationStart").val(_data_name);
            else
                $$("#stationEnd").val(_data_name);

            myApp.closeModal(".popup-flight_city");
        }
    });

    var base64 = new Base64();
    //换出发 - 到站
    $$(".station-change").on("click", function () {
        if ($$('#stationStart').val() != "" && $$('#stationEnd').val() != "") {
            var v_temp = $$('#stationStart').val();
            $$('#stationStart').val($$('#stationEnd').val());
            $$('#stationEnd').val(v_temp);
        }
    });

    //车站查询 
    $$("#btn_search_bus_ticket").on("click", function () {
        if ($$("#stationStart").val() == "") {
            myApp.alert('请输入起点站');
            return;
        }

        if ($$("#stationEnd").val() == "") {
            myApp.alert('请输入终点站');
            return;
        }

        mainView.router.loadPage("coachDetail.html?start=" + base64.encode($$("#stationStart").val()) + "&end=" + base64.encode($$("#stationEnd").val()));
    });
});

/* 客运查询结果  */
var contactsCallback_coachDetail = myApp.onPageInit('coachDetail', function (page) {

    var base64 = new Base64();
    var _start = ISS.utils.getParameter("start");
    var _end = ISS.utils.getParameter("end");
    if (page != undefined) {
        _start = page.query.start;
        _end = page.query.end;
    }

    var _url = ISS_Constants.Url.const_bus_ticket_station + "&from=" + base64.decode(_start) + "&to=" + base64.decode(_end);
    $$("#title").text(base64.decode(_start) + "-" + base64.decode(_end));
    myApp.showPreloader();
    $$.ajax({
        url: _url + "",
        crossDomain: true,
        dataType: "json",
        success: function (data, status) {
            myApp.hidePreloader();
            if (data.error_code == "0" && data.result && data.result.list.length > 0) {
                var searchTemplate = $$('#template-news-tab1').html();
                var compiledTemplate = Template7.compile(searchTemplate);
                var html = compiledTemplate(data);
                $$("#bus_ticket_list").html(html);
            }
            else {
                //没有数据
                $$("#bus_ticket_list").html("<li style='text-align:center;height:5em;line-height:5em;'>没有查到相关车次!</li>");
            }
        },
        error: function (xhr, status) {
            myApp.hidePreloader();
            $$("#bus_ticket_list").html("<li style='text-align:center;height:5em;line-height:5em;'>没有查到相关车次!</li>");
        }
    });
});

/* 初始化  */
function initData() {
    var href = window.location.href;
    if (href.indexOf(flight) > -1) {
        contactsCallback_flight.trigger();
    }
    else if (href.indexOf(flightDetail) > -1) {
        contactsCallback_flightDetail.trigger();
    }
    else if (href.indexOf(train) > -1) {
        contactsCallback_train.trigger();
    }
    else if (href.indexOf(trainDetail) > -1) {
        contactsCallback_trainDetail.trigger();
    }
    else if (href.indexOf(coach) > -1) {
        contactsCallback_coach.trigger();
    }
    else if (href.indexOf(coachDetail) > -1) {
        contactsCallback_coachDetail.trigger();
    }
}

initData();